package com.ruoyi.common.sdk.oos;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.ResponseMessage;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import lombok.extern.slf4j.Slf4j;

import java.io.*;

@Slf4j
public class AliyunOosClient {

    //key
    private  String accessKeyId;
    //secret
    private  String accessKeySecret;
    //连接节点
    private  String endpoint;
    //桶名称
    private  String bucketName;

    // 使用私有构造函数防止直接实例化
    private AliyunOosClient(Builder builder) {
        this.accessKeyId = builder.accessKeyId;
        this.accessKeySecret = builder.accessKeySecret;
        this.endpoint = builder.endpoint;
        this.bucketName = builder.bucketName;
    }

    // 内部静态Builder类
    public static class Builder {
        private  String accessKeyId;
        private  String accessKeySecret;
        private  String endpoint;
        private  String bucketName;

        public Builder() {
        }

        public Builder accessKeyId(String accessKeyId) {
            this.accessKeyId = accessKeyId;
            return this;
        }
        public Builder accessKeySecret(String accessKeySecret) {
            this.accessKeySecret = accessKeySecret;
            return this;
        }
        public Builder endpoint(String endpoint) {
            this.endpoint = endpoint;
            return this;
        }
        public Builder bucketName(String bucketName) {
            this.bucketName = bucketName;
            return this;
        }
        public AliyunOosClient build() {
           return new AliyunOosClient(this);
        }

    }

    /**
     * OOS 文件上传
     * @param filePath 本地文件路径【本地文件的完整路径，例如D:\\localpath\\examplefile.txt。】
     * @param objectName 上传到OOS的路径【完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt】
     */
    public void uploadFileToOSS(String filePath, String objectName) {
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
        try {
            InputStream inputStream = new FileInputStream(filePath);
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
            // 创建PutObject请求。
            PutObjectResult result = ossClient.putObject(putObjectRequest);
//            ResponseMessage response = result.getResponse();
        } catch (OSSException oe) {
            log.info("捕获到OSS异常，这意味着您的请求已到达OSS，");
            log.info("但是由于某些原因被拒绝了。");
            log.info("错误消息：" + oe.getErrorMessage());
            log.info("错误代码：" + oe.getErrorCode());
            log.info("请求ID：" + oe.getRequestId());
            log.info("主机ID：" + oe.getHostId());
        } catch (ClientException ce) {
            log.info("捕获客户端异常，这意味着客户端在尝试与OSS通信时遇到了严重内部问题，");
            log.info("比如无法访问网络。");
            log.info("错误消息：" + ce.getMessage());
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }


    /**
     * 下载文件
     * @param pathName  填写Object下载到本地的完整路径。
     * @param objectName  填写不包含Bucket名称在内的Object完整路径，例如testfolder/exampleobject.txt。
     */
    private void downloadFileFromOSS(String pathName, String objectName) {
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
        // 创建OSSClient实例。
        try {
            ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File(pathName));
        } catch (OSSException oe) {
            log.info("捕获到OSS异常，这意味着您的请求已到达OSS，");
            log.info("但是由于某些原因被拒绝了。");
            log.info("错误消息：" + oe.getErrorMessage());
            log.info("错误代码：" + oe.getErrorCode());
            log.info("请求ID：" + oe.getRequestId());
            log.info("主机ID：" + oe.getHostId());
        } catch (ClientException ce) {
            log.info("捕获客户端异常，这意味着客户端在尝试与OSS通信时遇到了严重内部问题，");
            log.info("比如无法访问网络。");
            log.info("错误消息：" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

    }

    @Override
    public String toString() {
        return "AliyunOosClient{" +
                "accessKeyId='" + accessKeyId + '\'' +
                ", accessKeySecret='" + accessKeySecret + '\'' +
                ", endpoint='" + endpoint + '\'' +
                ", bucketName='" + bucketName + '\'' +
                '}';
    }

}
